package com.quentinbeauperin.twitbird
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	
	public class UserInterface extends Sprite
	{
		public var life1:Bitmap;
		public var life2:Bitmap;
		public var life3:Bitmap;
		public var engine:Engine;
		public var game:Game;
		public var bird:Bird;
		public var obstacleGenerator1:ObstacleGenerator;
		public var obstacleGenerator2:ObstacleGenerator;
		public var countLives:int = new int;
		
		public function UserInterface()
		{
			super();
			createLives();
			countLives = 3;
			//addEventListener(Event.ENTER_FRAME,detectCollisions);
		}
		
		// Create lives
		private function createLives():void
		{
			life1 = new Assets.Life();
			life2 = new Assets.Life();
			life3 = new Assets.Life();
			life1.y = life2.y = life3.y = 410;
			life2.x = 40;
			life3.x = 80;
			addChild(life1);
			addChild(life2);
			addChild(life3);
		}
		
		// Detect collisions
		private function detectCollisions(event:Event):void
		{
			if (life1.hitTestObject(engine.bird)){
				countLives -= 1;
				deleteLife();
			}
			else if (bird.hitTestObject(obstacleGenerator2.obstacle)){
				countLives -= 1;
				deleteLife();
			}
		}
		
		// Delete life
		private function deleteLife():void
		{
			if (countLives == 2){
				removeChild(life3);
			}else if (countLives == 1){
				removeChild(life2);
			}else if (countLives == 0){
				engine.screenEngine = new ScreenEngine();
				addChild(engine.screenEngine);
			}
		}
	}
}